package jmu.cze.controller;

import jmu.cze.service.ProductSerivce;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
public class ProductController {
    private ProductSerivce productSerivce;

    @Resource(name = "productSerivceImpl")
    public void setProductSerivce(ProductSerivce productSerivce) {
        this.productSerivce = productSerivce;
    }

    @GetMapping("/")
    public String index() {
        return "redirect:/1";
    }

    @GetMapping("/{page}")
    public String getProducts(@PathVariable int page, Model model) {
        if(page<1) page=1;
        model.addAttribute("list", productSerivce.getProducts(page, 10));
        model.addAttribute("count",productSerivce.countAll());
        return "index";
    }

    @GetMapping("/search/{kw}")
    public String getProducts(@PathVariable("kw") String kw, Model model) {
        model.addAttribute("list", productSerivce.search(kw));
        model.addAttribute("count",productSerivce.countAll());
        return "index";
    }

    @RequestMapping("/detail/{id}")
    public String detail(@PathVariable int id, Model model) {
        model.addAttribute("product", productSerivce.findByID(id));
        return "detail";
    }

    @GetMapping("/getNote/{id}")
    public String note(@PathVariable int id, Model model){
        model.addAttribute("id", id);
        return "note";
    }

}